package com.zz.cc.business.data;

import android.content.Context;

import com.zz.cc.business.BusinessApplication;
import com.zz.cc.common.data.IFileManager;
import com.zz.common.tools.ImageLoader.IUrlLocalFilePathCreator;
import com.zz.common.utils.AlgorithmUtil;
import com.zz.common.utils.FileUtil;
import com.zz.common.utils.StorageUtil;

public final class FileManager extends IFileManager implements IUrlLocalFilePathCreator {
	
	private static final String FILE_NAME_QR_CODE = "qrCode.jpg";
	private static FileManager sInstance;
	
	private String mWorkPath;
	private String mIconCachePath;
	
	public static FileManager getInstance() {
		if(null == sInstance) {
			sInstance = new FileManager(BusinessApplication.getBaseApplication());
		}
		return sInstance;
	}
	
	@Override
	public String getWorkDir() {
		return mWorkPath;
	}
	
	public String getIconPathForUrl(String url) {
		String md5 = AlgorithmUtil.md5(url);
		String path = mIconCachePath + md5 + ICON_FILE_EXTENSION;
		return path;
	}
	
	@Override	//ImageLoader.IUrlLocalFilePathCreator
	public String createLocalFilePathForUrl(String url) {
		return getIconPathForUrl(url);
	}
	
	public String getQrCodePath() {
		return mWorkPath + FILE_NAME_QR_CODE;
	}
	
	private FileManager(Context c) {
		String path = StorageUtil.getSDCardDir();
		mWorkPath = path + PATH_ROOT + PATH_BUSINES;
		mIconCachePath = mWorkPath + PATH_ICON_CACHE;
		FileUtil.ensureDir(mIconCachePath);
		makeDirNoMedia(mIconCachePath);
	}


}
